var express = require('express');
var app = express();
var bodyParser = require('body-parser');
const path = require("path");
const Database = require("better-sqlite3");
// 创建 application/x-www-form-urlencoded 编码解析

app.use(bodyParser.urlencoded({
    extended: false ,
    parameterLimit: 10000,
    limit: 1024 * 1024 * 10
}))
app.use(express.json());
app.use(bodyParser.json());

//静态托管资源，否则无法进行访问
app.use(express.static("./public"));

//路由
var stuRouter = require('./routes/stu_data');
var checkloginRouter = require('./routes/checklogin');
var bookRouter = require('./routes/book_data');
var storeRouter = require('./routes/book_store');
var stulendRouter = require('./routes/stu_lend');
var stureturnRouter = require('./routes/stu_return');
var booklogoffRouter = require('./routes/book_logoff')
var fineRouter = require('./routes/stu_fine')

app.use((req, res, next) => {
    //针对跨域进行配置，允许任何源访问
    res.header('Access-Control-Allow-Origin', "*")
    // 允许前端请求中包含Content-Type这个请求头
    res.header('Access-Control-Allow-Headers', 'Content-Type')
    next()
})

//使用路由
app.use('/stu', stuRouter);
app.use('/book', bookRouter);
app.use('/login', checkloginRouter);
app.use('/store', storeRouter);
app.use('/lend', stulendRouter);
app.use('/return', stureturnRouter);
app.use('/logoff', booklogoffRouter);
app.use('/fine', fineRouter);

var server = app.listen(8081, function () {

    var host = server.address().address
    var port = server.address().port
    console.log(`服务器运行成功:http://localhost:${port}`)

})